/**
 * 不相邻的数据相互不受影响
 *  table合并行通用 */
export function mergeTableRow(data, merge) {
  if (!merge || merge.length === 0) {
    return data
  }
  merge.forEach((m) => {
    const mList = {}
    data = data.map((v, index) => {
      const rowVal = v[m]
      if (mList[rowVal] && mList[rowVal].newIndex === index) {
        mList[rowVal]['num']++
        mList[rowVal]['newIndex']++
        data[mList[rowVal]['index']][m + '-span'].rowspan++
        v[m + '-span'] = {
          rowspan: 0,
          colspan: 0
        }
      } else {
        mList[rowVal] = {
          num: 1,
          index: index,
          newIndex: index + 1
        }
        v[m + '-span'] = {
          rowspan: 1,
          colspan: 1
        }
      }
      return v
    })
  })
  return data
}

/**
 * 根据数据自动计算列宽
 *
 * @param {Object} data 数据
 * @param {Object} lable {'列名':列名长度}
 */
export function calcTableColWidth(data, lable) {
  const font_width = 14,
    num_width = 7
  let obj = {},
    obj_max = {},
    lable_max = {}
  data.map((item, index) => {
    for (var k in item) {
      let val = item[k],
        width = 120
      if (val instanceof Object) {

      } else {
        if (parseFloat(val).toString() == "NaN") {
          width = val && val.length * font_width
        } else { // 是数字
          width = val.toString().length * num_width
        }
        if (index == 0) {
          obj[k] = [width]
        } else {
          obj[k].push(width)
        }
      }
    }
  })
  // 遍历最大值
  for (var k in obj) {
    obj_max[k] = Math.max.apply(Math, obj[k]);
  }

  // lable宽度
  for (var k in lable) {
    let lable_width = lable[k] * font_width,
      obj_width = obj_max[k]
    if (obj_width > lable_width) {
      lable_width = obj_width
    }
    // 加上padding
    lable_max[k] = lable_width + 40
  }
  return lable_max
}

/**
 * 小计
 *
 * @param {Array} data 数据
 * @param {String} col_id 小计唯一标识字段
 * @param {String} col_name 小计字段
 * @param {String} col_dispName 显示"小计"的列名
 * @param {Array} lable 需要小计的字段 ['列名']
 */
export function tableRowSubtotal(data, col_id, col_name, col_dispName, lable) {

  if (data.length && col_id && col_name && lable.length) {
    const _col_id = data[0][col_id]
    let list = [],
      col = {
        [_col_id]: {}
      },
      before_col_id = _col_id,
      before_idx = 0

    lable.forEach((k) => {
      col[_col_id][k] = 0
    })

    // 遍历原始data
    data.map((item, index) => {
      let data_col_id = item[col_id]

      if (col[data_col_id]) {
        lable.forEach((k) => {
          col[data_col_id][k] = typeof(item[k]) === "number" ? parseFloat((col[data_col_id][k] + item[k])
            .toFixed(2)) : 0
        })
      } else {
        // push小计
        list.push(getBeforeItem(data, before_idx, col_name, col_dispName, col, before_col_id))
        // 新的col
        col[data_col_id] = {}
        lable.forEach((k) => {
          col[data_col_id][k] = typeof(item[k]) === "number" ? item[k] : 0
        })
      }
      list.push(item)
      before_col_id = data_col_id
      before_idx = index
    })
    // col大于2个的时候，push小计
    if (Object.keys(col).length > 1) {
      // push小计
      list.push(getBeforeItem(data, before_idx, col_name, col_dispName, col, before_col_id))
    }
    return list
  } else {
    return data
  }

}

function getBeforeItem(data, before_idx, col_name, col_dispName, col, before_col_id) {
  // 获取上一个item
  let before_item = Object.assign({}, data[before_idx])
  for (var k in before_item) {
    if (k === col_name) {

    } else if (k === col_dispName) {
      before_item[k] = '小计'
    } else {
      before_item[k] = typeof(col[before_col_id][k]) === "number" ? col[before_col_id][k] || 0 : ''
    }
  }
  return before_item
}
